define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    $('#c-parent_id').change(function () {
        var qid = $(this).val();
        // console.log(qid);return;
        $.ajax({
            url:"classes/tiku/son_cate",
            method: 'post',
            data: { 'qid': qid },
            success:function(data) {

                if(data.code == 100)
                {
                    var html = `<option value="0" selected>无</option>`;
                    var list = data.list;

                    for (var j = 0; j < list.length; j++)
                    {
                        html += `<option value="` + list[j].id + `">` + list[j].name + `</option>`;
                    }
                    // console.log(html);
                    $('#c-parent_id_t').html(html);
                }
                else if(data.code == 1)
                {
                    var html = `<option value="0" selected>无</option>`;
                    $('#c-parent_id_t').html(html);
                }
            },
            error: function(err) {
                console.log("失败了: " + err);
            }
        });
    });
    $('#c-parent_id_t').change(function () {
        var qid = $(this).val();
        // console.log(qid);return;
        $.ajax({
            url:"classes/tiku/son_cate",
            method: 'post',
            data: { 'qid': qid },
            success:function(data) {

                if(data.code == 100)
                {
                    var html = `<option value="0" selected>无</option>`;
                    var list = data.list;

                    for (var j = 0; j < list.length; j++)
                    {
                        html += `<option value="` + list[j].id + `">` + list[j].name + `</option>`;
                    }
                    // console.log(html);
                    $('#c-parent_id_tt').html(html);
                }
                else if(data.code == 1)
                {
                    var html = `<option value="0" selected>无</option>`;
                    $('#c-parent_id_tt').html(html);
                }
            },
            error: function(err) {
                console.log("失败了: " + err);
            }
        });
    });
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'classes/tiku/index' + location.search,
                    // add_url: 'classes/tiku/add',
                    // edit_url: 'classes/tiku/edit',
                    // del_url: 'classes/tiku/del',
                    // multi_url: 'classes/tiku/multi',
                    // import_url: 'classes/tiku/import',
                    table: 'classes_tiku',
                    dragsort_url: '',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        // {checkbox: true},
                        {field: 'id', title: __('Id')},
                        // {field: 'pid', title: __('Pid')},
                        // {field: 'type', title: __('Type'), operate: 'LIKE'},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'weigh', title: __('Weigh'), operate: false},
                        // {field: 'status', title: __('Status'), searchList: {"1":"正常","0":"隐藏"}, formatter: Table.api.formatter.status},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'question',
                                    title: __('查看题库'),
                                    text: __('查看题库'),
                                    extend:'data-area=["90%","90%"]',
                                    classname: 'btn  btn-xs btn-info btn-dialog',
                                    icon: 'fa fa-list',
                                    url: function (row) {
                                        return 'classes/tiku/question?id=' + row.id;
                                    }
                                },
                                {
                                    name: 'allocation',
                                    title: __('分配题库'),
                                    text: __('分配题库'),
                                    extend:'data-area=["50%","50%"]',
                                    classname: 'btn  btn-xs btn-info btn-dialog',
                                    icon: 'fa fa-list',
                                    url: function (row) {
                                        return 'classes/tiku/allocation?id=' + row.id;
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ],
                search: false,
                commonSearch: false
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        question: function () {
            var tiku_id = $("#tiku_id").val();
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'classes/tiku/question?tiku_id=' + tiku_id,
                    table: 'exam_question',
                }
            });

            var table = $("#table2");

            console.log($.fn.bootstrapTable.defaults.extend.index_url);
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search: false,
                showExport: false,//隐藏导出
                showToggle: false,//隐藏浏览模式
                showColumns: false,//隐藏显示隐藏模式
                searchFormVisible: false,//默认显示搜索表单
                commonSearch: false,
                columns: [
                    [
                        // {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        // {field: 'cate_id', title: __('Cate_id'), autocomplete: false, visible: false},
                        {field: 'cate_name', title: __('所属类型'), operate: false},
                        // {field: 'exam_type_id', title: __('Exam_type_id'), visible: false},
                        // {field: 'examtype.name', title: __('Exam_type_id'), operate: false},
                        {
                            field: 'kind',
                            title: __('试题类型'),
                            // searchList: {"JUDGE": __('Judge'), "SINGLE": __('Single'), "MULTI": __('Multi'), "FILL": __('Fill')},
                            formatter: function(value, row, index){
                                if(value == 'JUDGE')
                                {
                                    return '判断题';
                                }
                                else if(value == 'SINGLE')
                                {
                                    return '单选题';
                                }
                                else if(value == 'MULTI')
                                {
                                    return '多选题';
                                }
                                else if(value == 'FILL')
                                {
                                    return '填空题';
                                }
                                return value;
                            },
                            operate: false
                        },
                        {field: 'title', title: __('题目'), autocomplete: false, operate: 'LIKE', formatter: Table.api.formatter.content},
                        // {field: 'explain', title: __('Explain'), operate: false},
                        // {field: 'zql', title: __('正确率')},
                        // {field: 'dtrs', title: __('答题人数')},
                        {
                            field: 'difficulty',
                            title: __('难度'),
                            // searchList: {"EASY": __('Easy'), "GENERAL": __('General'), "HARD": __('Hard')},
                            formatter: function(value, row, index){
                                if(value == 'EASY')
                                {
                                    return '简单';
                                }
                                else if(value == 'GENERAL')
                                {
                                    return '普通';
                                }
                                else if(value == 'HARD')
                                {
                                    return '困难';
                                }
                                return value;
                            },
                            operate: false
                        },
                        {
                            field: 'answer', title: __('正确答案'), halign: 'center', align: 'left', operate: false, formatter: function (value, row, index) {
                                var answer = value;
                                if (row.kind == 'FILL') {
                                    try {
                                        answer = '';
                                        let fill_answers = JSON.parse(row.answer);
                                        for (let i = 0; i < fill_answers.length; i++) {
                                            answer += '填空位' + (i + 1) + '：' + fill_answers[i].answers.join('、') + '<br>';
                                        }
                                    } catch (e) {
                                        console.log('answer', index, answer, e);
                                        return value;
                                    }
                                }

                                console.log('answer', value, answer);
                                return answer;
                            }
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons:[
                                {
                                    name:"查看",
                                    text: __('查看'),
                                    title: __('查看'),
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    icon: 'fa fa-eye',
                                    url: 'classes/tiku/view',
                                }
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        allocation: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        },
    };
    return Controller;
});
